package com.yes_iot.service.impl;

import com.yes_iot.config.RegisterException;
import com.yes_iot.dao.LargeScreenConfigurationDao;
import com.yes_iot.entity.LargeScreenConfiguration;
import com.yes_iot.service.LargeScreenConfigurationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLIntegrityConstraintViolationException;
import java.util.List;
import java.util.Optional;
import java.util.UUID;

@Service
public class LargeScreenConfigurationServiceImpl extends AbstractEntityService implements LargeScreenConfigurationService {
    @Autowired
    private LargeScreenConfigurationDao largeScreenConfigurationDao;

    @Override
    public void save(LargeScreenConfiguration largeScreenConfiguration) throws RegisterException {
        try {
            largeScreenConfigurationDao.save(largeScreenConfiguration);
        }catch (Exception ignored){
            if (ignored instanceof DataIntegrityViolationException){
                throw new RegisterException("该园区已创建大屏项目");
            }
        }
    }


    @Override
    public void del(String id) {
        largeScreenConfigurationDao.deleteById(UUID.fromString(id));
    }

    @Override
    public LargeScreenConfiguration findById(UUID uuid) {
        Optional<LargeScreenConfiguration> byId = largeScreenConfigurationDao.findById(uuid);
        return byId.orElse(null);

    }

    @Override
    public List<LargeScreenConfiguration> findByIds(List<UUID> uuidList) {
        return largeScreenConfigurationDao.findByIds(uuidList);
    }

    @Override
    public List<LargeScreenConfiguration> findByFarmIds(List<Integer> farmIds) {
        return largeScreenConfigurationDao.findByFarmIds(farmIds);
    }

    @Override
    @Transactional
    public void basicUpdate(LargeScreenConfiguration largeScreenConfiguration) throws RegisterException {
        try {
            largeScreenConfigurationDao.basicUpdate(largeScreenConfiguration);
        }catch (Exception ignored){
            if (ignored instanceof DataIntegrityViolationException){
                throw new RegisterException("该园区已创建大屏项目");
            }
        }
    }

    @Override
    @Transactional
    public void visualUpdate(LargeScreenConfiguration largeScreenConfiguration) {
        largeScreenConfigurationDao.visualUpdate(largeScreenConfiguration);
    }
}
